﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;
using System.Net;
using System.IO;


namespace pst2imap
{
    class Program
    {
        // Uses recursion to enumerate Outlook subfolders.
        static private void EnumerateFolders(Outlook.MAPIFolder folder)
        {
            Outlook.Folders childFolders =
                folder.Folders;
            if (childFolders.Count > 0)
            {
                foreach (Outlook.Folder childFolder in childFolders)
                {
                    // Write the folder path.
                    Console.WriteLine(childFolder.FolderPath);
                    // Call EnumerateFolders using childFolder.
                    EnumerateFolders(childFolder);
                }
            }
        }


        static void Main(string[] args)
        {

            // get the Outlook Application Object
            Outlook.Application outlookApplication = new Outlook.Application();

            // get the namespace object
            Outlook.NameSpace olNamespace = outlookApplication.GetNamespace("MAPI");

            // Logon to Session, use elookinto profile.
            olNamespace.Logon("elookinto", "elookinto", false, true);
            //String mail = "elookinto@gmail.com";
            String mail = "elookinto@127.0.0.1"; //
            Outlook.MAPIFolder elookintoFolder = olNamespace.Folders[mail];//[];
            Outlook.MAPIFolder temp = null;
            EnumerateFolders(elookintoFolder);


            try
            {

                String pst = @"c:\temp\my-test2.pst";
                if (args.Length > 0) pst = args[0];
                FileInfo fi1 = new FileInfo(pst);
                if (!fi1.Exists)
                {
                    Console.WriteLine("no such file exists");
                    return;
                }
                String storeName = fi1.Name.Substring(0, fi1.Name.LastIndexOf("."));
                System.Diagnostics.Debug.WriteLine(storeName);
                try
                {
                    elookintoFolder.Folders["Deleted Items"].Folders[storeName].Delete();
                }
                catch (System.Runtime.InteropServices.COMException e)
                {
                    //Console.WriteLine(e.StackTrace);
                }
                try
                {

                    elookintoFolder.Folders["INBOX"].Folders[storeName].Delete();
                }
                catch (System.Runtime.InteropServices.COMException e)
                {
                    //Console.WriteLine(e.StackTrace);
                }
                try
                {
                    Outlook.MAPIFolder pstFolder = olNamespace.Stores[storeName].GetRootFolder();
                    olNamespace.RemoveStore(pstFolder);

                }
                catch (System.Runtime.InteropServices.COMException e)
                {
                    //Console.WriteLine(e.StackTrace);
                }

                //Console.WriteLine(pst);
                Outlook.MAPIFolder newPstFolder = null;
                try
                {
                    olNamespace.AddStore(pst);
                    Store addedStore = null;
                    foreach (Store store in olNamespace.Stores)
                    {
                        if (store.FilePath!= null && store.FilePath.Equals(pst))
                        {
                            addedStore = store;
                            break;
                        }
                    } 

                    Console.WriteLine("fileName=" + pst);
                    Console.WriteLine("Store Name=" + addedStore.DisplayName);
                    temp = elookintoFolder.Folders["INBOX"].Folders.Add(storeName, OlDefaultFolders.olFolderInbox);

                    newPstFolder = addedStore.GetRootFolder();

                    foreach (Folder subfolder in newPstFolder.Folders)
                    {
                        subfolder.CopyTo(temp);
                    }
                    Console.WriteLine("success");
                    return;
                }
                catch (System.Runtime.InteropServices.COMException e)
                {
                    //Console.WriteLine(e.StackTrace);
                    if (temp != null) temp.Delete();
                    Console.WriteLine("failed" + e.StackTrace);
                }
                finally
                {
                   
                    if (newPstFolder != null)
                      olNamespace.RemoveStore(newPstFolder);
                    //temp.Delete();
                    newPstFolder = null;
                }
               
                // MessageBox.Show("abc");
            }
            catch (System.Runtime.InteropServices.COMException ee)
            {
                //MessageBox.Show(ee.ToString());
                Console.WriteLine("failed" + ee.StackTrace);
            }
            finally
            {
                // logoff from namespace (session)
                olNamespace.Logoff();
                //release reference o outlook object
                outlookApplication = null;
            }
            Console.WriteLine("failed"); 
            // Console.Read();
        }
    }
}







// WebClient wc = new WebClient();
//String s = wc.DownloadString("http://localhost:9080/ilookinto/my/emailDeleteFolder.jsp?folder=" + storeName);
//Console.WriteLine(s + storeName);
//elookintoFolder.Folders["[Gmail]"].Folders["Trash"].Folders[storeName].Delete();